﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Text.RegularExpressions;

namespace UpdateProductVersion
{
    class Program
    {
        static void Main(string[] args) {
            //Take two command-line arguments
            if(args.Length != 2) {
                Console.WriteLine("usage: UpdateProductVersion.exe <assembly file> <config file>");
                Environment.Exit(1);
            }

            //Read the assmebly
            string assemblyPath = args[0];
            Version assemblyVersion = Assembly.LoadFile(assemblyPath).GetName().Version;
            Console.WriteLine("Assembly Verison: {0}", assemblyVersion);

            string configFilePath = args[1];
            string config = File.ReadAllText(configFilePath);
            Regex reg = new Regex("productVersion=.*");
            Match isMatch = reg.Match(config);
            if(!isMatch.Success) {
                Console.WriteLine("Could not update Product Version");
                Environment.Exit(1);
            }

            File.WriteAllText(configFilePath, reg.Replace(config, string.Format("productVersion={0}\r", assemblyVersion)));

            Console.WriteLine("Done Updating Product Version");
            //Console.ReadLine();
        }
    }
}
